package dao;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import vo.Msg;
import vo.Dingdan;

public class DingdanDao {
	Connection conn=null;
	PreparedStatement ps=null;
	Statement st=null;
	ResultSet rs=null;	
	String url = "jdbc:mysql://localhost:3306/shi?useUnicode=true&characterEncoding=utf8"; 
    String user = "root"; 
    String password = "root";
		
	Connection getConnection(){
		try {
		    conn = DriverManager.getConnection(url, user, password);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return conn;
	}
	
	void close(Connection conn){
		try {
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	void close(Connection conn,Statement ps){
		try {
			ps.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.close(conn);
	}
	
	void close(Connection conn,Statement ps,ResultSet rs){
		try {
			rs.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		this.close(conn, ps);
	}
	
	static{
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public List<Dingdan> getAll(){
		List<Dingdan> list=new ArrayList<Dingdan>();
		this.getConnection();
		try {
			ps=conn.prepareStatement("select * from dingdan");
			rs=ps.executeQuery();
			while(rs.next()){
				Dingdan ding=new Dingdan();
				ding.setDid(rs.getInt("did"));
				ding.setName(rs.getString("name"));
				ding.setUsername(rs.getString("username"));
				ding.setPhoto(rs.getString("photo"));
				ding.setMoney(rs.getString("money"));
				ding.setMid(rs.getInt("mid"));
				ding.setPeople(rs.getString("people"));
				ding.setTel(rs.getString("tel"));
				list.add(ding);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return list;
	}
	
	public List<Dingdan> getMyDingdan(String username){
		List<Dingdan> list=new ArrayList<Dingdan>();
		this.getConnection();
		try {
			ps=conn.prepareStatement("select * from dingdan where username='"+username+"'");
			rs=ps.executeQuery();
			while(rs.next()){
				Dingdan ding=new Dingdan();
				ding.setDid(rs.getInt("did"));
				ding.setName(rs.getString("name"));
				ding.setUsername(rs.getString("username"));
				ding.setPhoto(rs.getString("photo"));
				ding.setMoney(rs.getString("money"));
				ding.setMid(rs.getInt("mid"));
				ding.setPeople(rs.getString("people"));
				ding.setTel(rs.getString("tel"));
				list.add(ding);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}	
		return list;
	}
	
	
	public void insert(String name,String username,String photo,String money,int mid,String people,String tel){
		this.getConnection();
		try {
			ps= conn.prepareStatement("insert into dingdan(name,username,photo,money,mid,people,tel) values(?,?,?,?,?,?,?)");

			ps.setString(1, name);
			ps.setString(2, username);
			ps.setString(3, photo);
			ps.setString(4, money);
			ps.setInt(5, mid);
			ps.setString(6, people);
			ps.setString(7, tel);
			
			ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			this.close(conn, ps);
		}
	}
	
	
	public void delete(int did){
		this.getConnection();
		try {
			ps=conn.prepareStatement("delete from dingdan where did="+did);
			ps.execute();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			this.close(conn, ps);
		}		
	}

}
